//#include <string.h>
#pragma once
#include <time.h>

#include "version.h"
#include "yuv.h"
#include "ParameterViewInterpolation.h"
#include "ViewInterpolation.h"
#include "DongTest.h"
#include "StereoParameters.h"
#include <stdio.h>
#include "StereoIO.h"
#include "Error.h"
#include "highgui.h"

#include "iostream"
#include "IniWriter.h"
#include "IniReader.h"


#ifndef WIN32
#define BYTE unsigned char
#endif

int main(int argc ,  char *argv[]) 
{

 CIniReader iniReader(".\\Logger.ini");
 char *cfgFile = iniReader.ReadString("Setting", "configFile", "");   
 bool useStereo = iniReader.ReadBoolean("Setting", "useStereo", false); 
#ifdef OUTPUT_COMPUTATIONAL_TIME
  clock_t start, finish, first;
  first = start = clock();
#endif

CStereoIO s;
if (useStereo == true ){
    

    try
    {
		argc = 3;
		argv[1] = "script";
		argv[2] ="exp_dispatch.txt";
        s.InterpretCommandLine(argc, argv);
		char fileName[50];
		for (int i = 0; i < s.depthImagesRef.size(); i++) {
			sprintf(fileName, "stereo_output/img%3dL.png", i);
			cvSaveImage(fileName, s.depthImagesRef[i]);
		}
		
		printf("done creating depth image");
    }
    catch (CError &err)
    {
        fprintf(stderr, err.message);
        fprintf(stderr, "\n");
        return -1;
    }
}
#if 1
  unsigned int n;
  CParameterViewInterpolation  cParameter;
  CViewInterpolation cViewInterpolation;
  CIYuv yuvBuffer;
  argc = 2;
  argv[1] = cfgFile;


  printf("View Synthesis Reference Software (VSRS), Version %.1f\n", VERSION);
  printf("     - MPEG 3DV, August 2009\n\n");
  if ( cParameter.Init( argc, argv ) != 1 ) return 0;
  

  if(!cViewInterpolation.Init(cParameter)) return 10;

  if(!yuvBuffer.Resize(cParameter.getSourceHeight(), cParameter.getSourceWidth(), 420)) return 2;

 
#ifdef OUTPUT_COMPUTATIONAL_TIME
  finish = clock();
  printf( "Initialization: %.4f sec\n", (double)(finish - start) / CLOCKS_PER_SEC);
  start = finish;
#endif

  for(n = cParameter.getStartFrame(); n < cParameter.getStartFrame() + cParameter.getNumberOfFrames(); n++) 
  {
    printf("frame number = %d ", n);
	if(useStereo==true){
		if(!cViewInterpolation.DoViewInterpolation( s.depthImagesRef[0],s.depthImagesRef[1])) break;
	}
	else{
		if(!cViewInterpolation.DoViewInterpolation( NULL,NULL)) break;
	}
	
#ifdef OUTPUT_COMPUTATIONAL_TIME
    finish = clock();
    printf("->End (%.4f sec)\n", (double)(finish - start) / CLOCKS_PER_SEC);
    start = finish;
#else
    printf("->End\n");
#endif
	break;
  } // for n

 
#ifdef OUTPUT_COMPUTATIONAL_TIME
  finish = clock();
  printf("Total: %.4f sec\n", ((double)(finish-first))/((double)CLOCKS_PER_SEC));
#endif

#endif

  return 0;
}

